package com.app.abby.perfectweather.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.abby.perfectweather.R;
import com.app.abby.perfectweather.base.BaseActivity;
import com.app.abby.perfectweather.base.BaseActivity2;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebActivity extends BaseActivity2 {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_root)
    RelativeLayout rlTitleRoot;

    @Override
    public int getLayoutID() {
        return R.layout.activity_web;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initView(Bundle savedInstanceState) {
        String web_url = getIntent().getStringExtra("web_url");
        boolean isTitle = getIntent().getBooleanExtra("isTitle", true);
        Log.d("test1", isTitle+"");
        if(!isTitle){
            rlTitleRoot.setVisibility(View.GONE);
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return true;
            }
        });
//        rlTitleRoot.setVisibility(View.GONE);
        tvTitle.setText("详情");
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDomStorageEnabled(true);
        webSettings.setGeolocationEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try{
                    if (url.contains("platformapi/startapp")) {
                        try {
                            Uri uri = Uri.parse(url);
                            Intent intent;
                            intent = Intent.parseUri(url,
                                    Intent.URI_INTENT_SCHEME);
                            intent.addCategory("android.intent.category.BROWSABLE");
                            intent.setComponent(null);
                            // intent.setSelector(null);
                            startActivity(intent);
                        } catch (Exception e) {

                        }
                    } else {
                        if( url.startsWith("http://") || url.startsWith("https://") ) {
                            return false;
                        }
                        Intent intent = new Intent();
                        intent.setData(Uri.parse(url));
                        startActivity(intent);
                        //如果不需要其他对点击链接事件的处理返回true，否则返回false
                        return true;
//                    view.loadUrl(url);
                    }
                    return true;
                }catch (Exception e){
                    return true;
                }

            }
        });
        webView.loadUrl(web_url);
        ivBack.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            webView.goBack();
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
